package com.zjbbkj.baibai.common.utils.wanli;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;

/**
 * json/map处理工具类
 *
 * @program: boot-parent
 * @author: Mr.WanLi
 * @create: 2019-06-19 15:36
 **/
@SuppressWarnings("unchecked")
public class JSONUtils {

    private static final Logger log = LoggerFactory.getLogger(JSONUtils.class);

    /**
     * JSON对象转JSON串
     *
     * @return String
     * @Author WanLi
     * @Date 10:40 2019/6/20
     * @Param Object
     **/
    public static String JsonObjectToJsonStr(Object o) {
        String json = "";
        try {
            json = JSONObject.fromObject(o).toString();
            if (!StringUtils.isBlank(json)) {
                return json;
            }
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return json;
    }


    /**
     * JSON串转JSON对象
     * @param json
     * @return JSONObject
     */
    public static JSONObject JsonStrToJsonObject(String json) {
        JSONObject obj = null;
        try {
            obj = JSONObject.fromObject(json);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return obj;
    }

    /**
     * @Description MAP转JSON对象
     *
     * @return JSONObject
     * @Author WanLi
     * @Date 10:47 2019/6/20
     * @Param
     **/
    public static JSONObject MapToJsonObject(Map map) {
        JSONObject obj = null;
        try {
            obj = JSONObject.fromObject(map);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return obj;
    }

    /**
     * @return String
     * @Author WanLi
     * @Param s
     * @Date 10:30 2019/6/28
     */
    public static String strToJSONStr(String s) {
        String json = null;
        try {
            Map<String, String> spMap = new LinkedHashMap<String, String>();
            String[] all = s.split("/");
            for (String arr : all) {
                String[] split = arr.split(":");
                spMap.put(split[0], split[1]);
            }
            json = JsonObjectToJsonStr(MapToJsonObject(spMap));
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return json;
    }

    public static Map<String, Object> parseJSON2Map(String jsonStr) {
        Map<String, Object> map = new HashMap<String, Object>();
        //最外层解析
        JSONObject json = JSONObject.fromObject(jsonStr);
        for (Object k : json.keySet()) {
            Object v = json.get(k);
            //如果内层还是数组的话，继续解析
            if (v instanceof JSONArray) {
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                Iterator<JSONObject> it = ((JSONArray) v).iterator();
                while (it.hasNext()) {
                    JSONObject json2 = it.next();
                    list.add(parseJSON2Map(json2.toString()));
                }
                map.put(k.toString(), list);
            } else {
                map.put(k.toString(), v);
            }
        }
        return map;
    }

    /**  objMap转strMap
     * @param map
     * @return
     */
    public static Map<String,String> ObjMapStrMap(Map<String,Object> map){
        Map tempMap = map;
        Map<String,String> destMap = tempMap;
        return destMap;
    }
}


